﻿using Microsoft.Extensions.Logging;
using NetCore.IRepository.User;
using NetCore.Model.Enity;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCore.Repository.User
{
    public class UserRepository : BaseRepository<SysUser>, IUserRepository
    {
        private readonly ILogger<UserRepository> logger;

        public UserRepository(ILogger<UserRepository> Logger)
        {
            logger = Logger;
        }

        public async Task<int> GetCount()
        {
            var i = await Task.Run(() => Db.Queryable<SysUser>().CountAsync());
            logger.LogError("GetCount发生了错误");
            return i;
        }

        public async Task<long> AddEquipmentInfo(SysEquipmentinfo sysEquipmentinfo)
        {
            try
            {
                //事务
                Db.BeginTran();

                //处理代码
                int count = await Db.Insertable(new SysEquipmentinfo()
                {
                    Number = sysEquipmentinfo.Number,
                    Name = sysEquipmentinfo.Number,
                    CreateDate = sysEquipmentinfo.CreateDate,
                    Equipmenttypeid = sysEquipmentinfo.Equipmenttypeid
                }).ExecuteCommandAsync();

                //事务提交
                Db.CommitTran();
                return count;
            }
            catch (Exception ex)
            {
                //数据回滚
                Db.RollbackTran();
                //打印错误日志
                logger.LogError(ex, ex.Message);
                return 0;
            }
        }

        public List<OrderLayerwindingmachineequipment> GetOrder_LayerwindingmachineEquipmentInfo()
        {
            List<OrderLayerwindingmachineequipment> orderLayerwindingmachineequipments = Db.Queryable<OrderLayerwindingmachineequipment>().ToList();
            return orderLayerwindingmachineequipments;
        }

        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <returns></returns>
        public async Task<List<int>> GetUserid()
        {
            List<int> list = new List<int>();
            list = await Db.Queryable<SysUser>().Select(i => i.UserId).ToListAsync();
            return list;
        }
    }
}